package com.app.controller;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.app.entity.*;
import com.app.service.*;
import com.app.utils.MD5;
import com.app.utils.MainUtils;

@Controller
@RequestMapping("/app")
public class HomeController {

    @Resource
    UsersService userService;

    @Resource
    GoodsService gService;

    @Resource
    InfocateService cService;
    @Resource
    NewsService nService;

    @RequestMapping("/news")
    public String news(String cateId, Model model) {

        List<News> nList = nService.getAllNews();
        List<News> nList1 = new LinkedList<News>();
        if (cateId != null) {
            for (News n : nList) {
                if (n.getUid().equals(cateId))
                    nList1.add(n);
            }
            model.addAttribute("nlist", nList1);
        } else {
            model.addAttribute("nlist", nList);
        }

        model.addAttribute("clist", cService.getAllInfocate());
        List<Cates> cates = caService.getByPid("0");
        model.addAttribute("cates", getChild(cates));
        return "home/news";
    }

    @Resource
    OrdertabService oService;

    @RequestMapping("/buy")
    public String buy(Model model, int id, HttpSession session) {
        if (session.getAttribute("user") == null) {
            return "redirect:index";
        }
        Goods g = gService.getGoodsById(id);
        Ordertab m = new Ordertab();
        m.uid = session.getAttribute("userid") + "";
        m.created = MainUtils.getTime();
        m.pid = id + "";
        m.num = 1 + "";
        m.price = g.price;


        oService.insert(m);
        model.addAttribute("message", "Submit ok");
        return "redirect:shop";
    }


    @RequestMapping("/shop")
    public String shop(Model model) {

        List<Goods> list = gService.getAllGoods();
        List<Goods> list1 = new LinkedList<Goods>();
        if (list.size() > 0) {
            for (Goods g : list) {
                g.setU(userService.getUsersById(Integer.parseInt(g.getUid())));
                list1.add(g);
            }
        }
        model.addAttribute("list", list1);
        model.addAttribute("clist", cService.getAllInfocate());
        return "home/shop";
    }


    @RequestMapping("/showNews")
    public ModelAndView showNews(int id, Model model) {
        ModelAndView mav = new ModelAndView("home/showNews");
        News article = nService.getNewsById(id);
        model.addAttribute("model", article);
        model.addAttribute("clist", cService.getAllInfocate());
        return mav;
    }


    @RequestMapping("/index")
    public String index(HttpSession session, Model model) {

        List<News> nList = nService.getAllNews();
        Random r = new Random();
        int index = r.nextInt(nList.size());
        int index2 = r.nextInt(nList.size());

        News topNews = nList.get(index);
        News fdNews = nList.get(index2);


        List<Goods> gList = gService.getAllGoods();
        int index3 = r.nextInt(gList.size());
        int index4 = r.nextInt(gList.size());
        int index5 = r.nextInt(gList.size());
        int index6 = r.nextInt(gList.size());
        Goods topGoods = gList.get(index3);
        Goods midGoods = gList.get(index4);
        Goods midGoods1 = gList.get(index5);
        Goods midGoods2 = gList.get(index6);


        model.addAttribute("n1", topNews);
        model.addAttribute("n2", fdNews);
        model.addAttribute("g1", topGoods);
        model.addAttribute("g2", midGoods);
        model.addAttribute("g3", midGoods1);
        model.addAttribute("g4", midGoods2);

        model.addAttribute("clist", cService.getAllInfocate());
        model.addAttribute("nlist", nList);
        model.addAttribute("glist", gList);

        List<Cates> cates = caService.getByPid("0");
        System.out.println("sys:");
        System.out.println(cates.size());
        model.addAttribute("cates", getChild(cates));
        return "home/index";
    }

    @Resource
    CatesService caService;

    public List<Cates> getChild(List<Cates> list) {

        if (list.size() > 0) {
            for (Cates category : list) {

                List<Cates> subList = caService.getByPid(category.getId() + "");
                category.setSubs(getChild(subList));
            }
        }

        return list;
    }


}
